package charts;

import org.jetbrains.annotations.NotNull;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import utils.DBUtil;

import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class PieChart {
    ChartPanel frame1;

    public PieChart(int id) {
        DefaultPieDataset data = getDataSet(id);
        JFreeChart chart = ChartFactory.createPieChart3D("交费情况", data, true,
                false, false);
        // 设置百分比
        PiePlot pieplot = (PiePlot) chart.getPlot();
        DecimalFormat df = new DecimalFormat("0.00%");// 获得一个DecimalFormat对象，主要是设置小数问题
        NumberFormat nf = NumberFormat.getNumberInstance();// 获得一个NumberFormat对象
        StandardPieSectionLabelGenerator sp1 = new StandardPieSectionLabelGenerator(
                "{0}  {2}", nf, df);// 获得StandardPieSectionLabelGenerator对象
        pieplot.setLabelGenerator(sp1);// 设置饼图显示百分比

        // 没有数据的时候显示的内容
        pieplot.setNoDataMessage("无数据显示");
        pieplot.setCircular(false);
        pieplot.setLabelGap(0.02D);

        pieplot.setIgnoreNullValues(true);// 设置不显示空值
        pieplot.setIgnoreZeroValues(true);// 设置不显示负值
        frame1 = new ChartPanel(chart, true);
        chart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));// 设置标题字体
        PiePlot piePlot = (PiePlot) chart.getPlot();// 获取图表区域对象
        piePlot.setLabelFont(new Font("宋体", Font.BOLD, 10));// 解决乱码
        chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 10));
    }


    private static @NotNull DefaultPieDataset getDataSet(int id) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        if (id == 0) {
            String sql2 = "SELECT count(Seeker_remark=0 OR null) as '已交齐',count(Seeker_remark = 1 OR NULL) as '未全部交齐',count(Seeker_remark=2 OR NULL) as '未交' FROM fee_seeker;";
            Statement stmt = DBUtil.getStatement();
            try {
                ResultSet rs1 = stmt.executeQuery(sql2);
                while (rs1.next()) {
                    dataset.setValue("交齐", rs1.getInt(1));
                    dataset.setValue("未全部交齐", rs1.getInt(2));
                    dataset.setValue("未交", rs1.getInt(3));
                    return dataset;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else if (id == 1) {
            String sql1 = "SELECT count(remark=0 OR null) as '已交齐',count(remark = 1 OR NULL) as '未全部交齐',count(remark=2 OR NULL) as '未交' FROM fee_employer;";
            Statement stmt = DBUtil.getStatement();
            try {
                ResultSet rs1 = stmt.executeQuery(sql1);
                while (rs1.next()) {
                    dataset.setValue("交齐", rs1.getInt(1));
                    dataset.setValue("未全部交齐", rs1.getInt(2));
                    dataset.setValue("未交", rs1.getInt(3));
                    return dataset;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return dataset;
    }

    public ChartPanel getChartPanel() {
        return frame1;
    }
}
